package route

import (
	"github.com/gorilla/mux"
	"net/http"
)

type HandlerFunc func(ctx Context)

type Var map[string]string

type Context interface {
	GetResponse() http.ResponseWriter
	GetRequest() *http.Request
	GetVar() Var
	GetVarName(name string) string
}

type defaultContext struct {
	response http.ResponseWriter
	request *http.Request
	vars Var
}

func NewContext(response http.ResponseWriter, request *http.Request) Context {
	vars := mux.Vars(request)
	return &defaultContext{
		response: response,
		request: request,
		vars: vars,
	}
}

func (this *defaultContext) GetResponse() http.ResponseWriter {
	return this.response
}

func (this *defaultContext) GetRequest() *http.Request {
	return this.request
}

func (this *defaultContext) GetVar() Var {
	newVar := make(Var, len(this.vars))
	for k, v := range this.vars {
		newVar[k] = v
	}
	return newVar;
}

func (this *defaultContext) GetVarName(name string) string {
	val, ok := this.vars[name]
	if ok {
		return val
	}
	return ""
}




